package com.guide.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.guide.bizdto.UserInfoParam;
import com.guide.dao.GuideApplyDao;
import com.guide.dao.GuideDao;
import com.guide.dao.TravelTeamDao;
import com.guide.domain.Account;
import com.guide.domain.Guide;
import com.guide.domain.GuideApply;
import com.guide.domain.TravelTeam;
import com.guide.enums.ErrorCodeEnum;
import com.guide.exception.ServiceException;
import com.guide.service.GuideService;
import com.guide.utils.ConstantUtils;

@Service(value="guideService")
public class GuideServiceImpl implements GuideService {
	private static final Logger logger = LoggerFactory
			.getLogger(GuideServiceImpl.class);
	//private Date nowTime  = new Date();
	@Autowired
	private GuideDao guideDao;
	
	@Autowired
	private GuideApplyDao  guideApplyDao;
	@Autowired
	private TravelTeamDao travelTeamDao;

	@Override
	@Transactional
	public void applyTravelTeam(Account account, Long teamId) {
		// TODO Auto-generated method stub
		TravelTeam travelTeam = travelTeamDao.getTravelTeamById(teamId);
		if(travelTeam.getGuidestatus()!=ConstantUtils.GUIDE_STATUS_0){
			throw new ServiceException(ErrorCodeEnum.CODE_GUIDE_TEAM_ERROR);
		}
		Guide guide = guideDao.getGuideByUserId(account.getUserId());
		GuideApply guideApply= guideApplyDao.getGuideApplyByTeamIdAndGuideId(guide.getId(), teamId);
		if(guideApply!=null) {
			throw new ServiceException(ErrorCodeEnum.CODE_GUIDE_TEAM_APPLYED_ERROR);
		}
		guideApply = new GuideApply();
		guideApply.setGuideId(guide.getId());
		guideApply.setUserId(account.getUserId());
		guideApply.setTeamId(teamId);
		guideApply.setStatus(ConstantUtils.GUIDE_STATUS_0);
		guideApply.setCtime(new Date());
		guideApply.setMtime(new Date());
		guideApplyDao.insertGuideApply(guideApply);
		travelTeam.setApplycount(travelTeam.getApplycount()+1);
		travelTeamDao.updateTravelTeam(travelTeam);
		
	}

	@Override
	public List<Guide> getApplyGuidesByTeamId(Long teamId) {
		// TODO Auto-generated method stub
		List<Guide> guides = new ArrayList<Guide>();
		List<GuideApply> guideApplys = guideApplyDao.getApplyGuidesByTeamId(teamId);
		if(!CollectionUtils.isEmpty(guideApplys)) {
			for(GuideApply guideApply:guideApplys) {
				Guide guide = guideDao.getGuideByUserId(guideApply.getUserId());
				if(guide!=null){
					guides.add(guide);
				}
			}
		}
		return guides;
	}

	@Override
	public Guide getApplyGuideByConfirm(Long teamId) {
		// TODO Auto-generated method stub
		GuideApply guideApply = guideApplyDao.getApplyGuideByConfirm(teamId);
		if(guideApply!=null) {
			return guideDao.getGuideById(guideApply.getGuideId());
		}else{
			throw new ServiceException(ErrorCodeEnum.SYSTEM_ERROR);
		}
	}
	

	@Override
	@Transactional
	public void acceptApplyGuide(Long teamId, Long guideId) {
		GuideApply guideApply = new GuideApply();
		guideApply.setTeamId(teamId);
		guideApply.setStatus(ConstantUtils.GUIDE_STATUS_2);
		guideApplyDao.updateApplyGuide(guideApply);
		GuideApply guideApply1 = new GuideApply();
		guideApply1.setTeamId(teamId);
		guideApply1.setGuideId(guideId);
		guideApply1.setStatus(ConstantUtils.GUIDE_STATUS_1);
		guideApplyDao.updateApplyGuide(guideApply1);
		TravelTeam travelTeam = new TravelTeam();
		travelTeam.setId(teamId);
		travelTeam.setGuidestatus(ConstantUtils.GUIDE_STATUS_1);
		travelTeamDao.updateTravelTeam(travelTeam);
		
	}

	@Override
	public Guide getGuideByUserId(Long userId) {
		// TODO Auto-generated method stub
		return guideDao.getGuideByUserId(userId);
	}

	@Override
	public Guide getGuideById(Long id) {
		// TODO Auto-generated method stub
		return guideDao.getGuideById(id);
	}


	
	




}
